﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TouristAgencyDataLayer;

namespace TouristAgencyWinApp
{
    public partial class ToursForm : Form
    {
        private Tour selectedTour = null;

        public ToursForm()
        {
            InitializeComponent();
            WinLoad();
        }

        private void WinLoad()
        {
            tourBindingSource.DataSource = Tours.GetTours();
            destinationBindingSource.DataSource = Destinations.GetDestinations();
            meanBindingSource.DataSource = MeansOfTransports.GetMeansOfTransport();
            boardBindingSource.DataSource = Boards.GetBoards();
            this.boardCB.SelectedItem = null;
            this.destCB.SelectedItem = null;
            this.meanCB.SelectedItem = null;
        }

        private void ToursForm_Load(object sender, EventArgs e)
        {        
        }

        private void toursGV_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != this.toursGV.Rows.Count - 1)
            {
                selectedTour = Tours.GetTourById(((Tour)this.toursGV.Rows[e.RowIndex].DataBoundItem).TourId);
                if (selectedTour.MeanOfTransportId != null)
                {
                    this.meanCB.SelectedValue = selectedTour.MeanOfTransportId;
                }
                else
                {
                    this.meanCB.SelectedItem = null;
                }
                this.destCB.SelectedValue = selectedTour.DestinationId;
                if (selectedTour.BoardId != null)
                {
                    this.boardCB.SelectedValue = selectedTour.BoardId;
                }
                else
                {
                    this.boardCB.SelectedItem = null;
                }
                this.startDateTimePicker.Value = selectedTour.StartDate;
                this.endDateTimePicker.Value = selectedTour.EndDate;
                this.descrTxt.Text = selectedTour.Description;
                this.priceTxt.Text = selectedTour.Price.ToString();
                var selectedHF = ToursHotelFacilities.GetTourHotelFacolities(selectedTour.TourId);
                this.hotelFCheckedListBox.Items.Clear();
                foreach (var hf in HotelFacilities.GetHotelFacilities())
                {
                    if (selectedHF.Contains(hf))
                    {
                        this.hotelFCheckedListBox.Items.Add(hf, true);
                    }
                    else
                    {
                        this.hotelFCheckedListBox.Items.Add(hf, false);
                    }
                }
            }
            else
            {
                selectedTour = null;
                this.boardCB.SelectedItem = null;
                this.destCB.SelectedItem = null;
                this.meanCB.SelectedItem = null;
                this.startDateTimePicker.Value = DateTime.Now;
                this.endDateTimePicker.Value = DateTime.Now;
                this.descrTxt.Text = null;
                this.priceTxt.Text = "0";

                this.hotelFCheckedListBox.Items.Clear();
                foreach (var hf in HotelFacilities.GetHotelFacilities())
                {
                    this.hotelFCheckedListBox.Items.Add(hf, false);
                }
            }
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (selectedTour != null)
            {
                int? mean;
                if (this.meanCB.SelectedValue == null)
                {
                    mean = null;
                }
                else
                {
                    mean = (int)this.meanCB.SelectedValue;
                }
                int? board;

                if (this.boardCB.SelectedValue == null)
                {
                    board = null;
                }
                else
                {
                    board = (int)this.boardCB.SelectedValue;
                }

                Tours.UpdateTour(selectedTour.TourId, (int)this.destCB.SelectedValue, this.startDateTimePicker.Value,
                    this.endDateTimePicker.Value, mean, board,
                    this.descrTxt.Text, Convert.ToDecimal(this.priceTxt.Text));

                ToursHotelFacilities.DeleteAllHotelFacilitiesOnTour(selectedTour.TourId);
                foreach (HotelFacility item in this.hotelFCheckedListBox.CheckedItems)
                {
                    ToursHotelFacilities.CreateTourHotelFacility(selectedTour.TourId, item.HotelFacilityId);
                }

                MessageBox.Show("Changes have been saved");
            }
            else
            {
                int? mean;
                if (this.meanCB.SelectedValue == null)
                {
                    mean = null;
                }
                else
                {
                    mean = (int)this.meanCB.SelectedValue;
                }
                int? board;

                if (this.boardCB.SelectedValue == null)
                {
                    board = null;
                }
                else
                {
                    board = (int)this.boardCB.SelectedValue;
                }

                if (this.destCB.SelectedValue != null && !string.IsNullOrEmpty(this.descrTxt.Text) && !string.IsNullOrEmpty(this.priceTxt.Text))
                {
                    int id = Tours.CreateTour((int)this.destCB.SelectedValue, this.startDateTimePicker.Value,
                        this.endDateTimePicker.Value, mean, board,
                        this.descrTxt.Text, Convert.ToDecimal(this.priceTxt.Text));


                    if (id != -1)
                    {
                        foreach (HotelFacility item in this.hotelFCheckedListBox.CheckedItems)
                        {
                            ToursHotelFacilities.CreateTourHotelFacility(id, item.HotelFacilityId);
                        }
                    }

                    MessageBox.Show("New your has been added");
                }
                else
                {
                    MessageBox.Show("There is some invalid information!");
                }

            }
            WinLoad();
        }

        private void delBtn_Click(object sender, EventArgs e)
        {
            if (selectedTour != null)
            {
                Tours.DeleteTour(selectedTour.TourId);
                WinLoad();
            }
        }

        private void toursGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
